local M = {}

function M:new()
	M.ffi = require("ffi")
	M.ffi.cdef[[
		void jieba_init(const char* dict_path,
				  const char* hmm_path,
				  const char* user_path,
				  const char* idf_path,
				  const char* stop_words_path);

		const char* jieba_cut(const char* text);
	]]

	--local path_sep = package.config:sub(1,1) -- "\" or "/"
	--local dll_dir = vim.fn.stdpath('config').."\\lua\\x_cmp\\bin\\"

	local info = debug.getinfo(1, 'S') -- 获取脚本路径
	local script_path = info.source:sub(2) -- 去掉前面的 '@'
	local dir = script_path:match("(.*[/\\])")
	local dll_dir = dir .. "bin/"
	M.jieba = M.ffi.load(dll_dir .. "x_cmp_jieba.dll")

	-- 初始化
	M.jieba.jieba_init(
		dll_dir.."dict/jieba.dict.utf8",
		dll_dir.."dict/hmm_model.utf8",
		dll_dir.."dict/user.dict.utf8",
		dll_dir.."dict/idf.utf8",
		dll_dir.."dict/stop_words.utf8"
	)
	return setmetatable({}, {__index = M})
end

function M:cut(to_cut)
    -- 如果to_cut内容为空，返回空表
    if to_cut == "" then
        return ""
    end

	-- 空格分隔的长字符串
	local result = M.ffi.string(M.jieba.jieba_cut(to_cut))
	return result
end

return M

